<template>
    <div class="add-node-btn-box">
        <div class="add-node-btn">
            <el-popover placement="right" v-model:visible="visible" trigger="click"
                v-if="childNode?.type != FlowNodeTypeEnum.Idea">
                <div class="add-node-popover-body">
                    <ul>
                        <li @click="addNode(FlowNodeTypeEnum.Audit)">
                            <el-space>
                                <img src="/@/assets/images/easyFlow/audit2.png" />
                                <span>审批节点</span>
                            </el-space>
                        </li>
                        <li @click="addNode(FlowNodeTypeEnum.Copy)">
                            <el-space>
                                <img src="/@/assets/images/easyFlow/copy2.png" />
                                <span>抄送节点</span>
                            </el-space>
                        </li>
                        <li @click="addNode(FlowNodeTypeEnum.Route)">
                            <el-space>
                                <img src="/@/assets/images/easyFlow/condition.png" />
                                <span>条件分支</span>
                            </el-space>
                        </li>
                        <li @click="addNode(FlowNodeTypeEnum.Idea)"
                            v-if="node != null && node?.type == FlowNodeTypeEnum.Audit">
                            <el-space>
                                <img src="/@/assets/images/easyFlow/auditResult.png" />
                                <span>审批意见</span>
                            </el-space>
                        </li>
                    </ul>
                </div>
                <template #reference>
                    <el-button circle>
                        <img src="/@/assets/images/easyFlow/addCondition.png" />
                    </el-button>
                </template>
            </el-popover>
        </div>
    </div>
</template>
<script setup lang="ts">
import { ref } from 'vue'
import { ElSpace } from 'element-plus';
import { FlowNodeTypeEnum } from '/@/enums/workFlowEnum';
import { WorkFlowNode } from '/@/types/workflow';
import { uuid } from '/@/utils/uuid';

const childNode = defineModel<Nullable<WorkFlowNode>>('childNode');
const node = defineModel<Nullable<WorkFlowNode>>('node');
let visible = ref<boolean>(false)

const addNode = (type) => {
    let data: Nullable<WorkFlowNode> = null;
    let nodeId = uuid();
    let parentId = node.value?.nodeId ?? null;
    switch (type) {
        case FlowNodeTypeEnum.Audit:
            data = new WorkFlowNode(nodeId, parentId, '审批人', FlowNodeTypeEnum.Audit, childNode?.value ?? null);
            break;
        case FlowNodeTypeEnum.Copy:
            data = new WorkFlowNode(nodeId, parentId, '抄送人', FlowNodeTypeEnum.Copy, childNode?.value ?? null);
            break;
        case FlowNodeTypeEnum.Route:
            data = new WorkFlowNode(nodeId, parentId, '条件分支', FlowNodeTypeEnum.Route, childNode?.value ?? null);
            var fistCondition = new WorkFlowNode(uuid(), nodeId, '条件1', FlowNodeTypeEnum.Condition, null, [], 1);
            var twoCondition = new WorkFlowNode(uuid(), nodeId, '默认流程', FlowNodeTypeEnum.Condition, null, [], 2, "未满足时其他条件时，将进入默认流程");
            twoCondition.isDefault = true;
            data.conditionNodes.push(fistCondition, twoCondition);
            break;
        case FlowNodeTypeEnum.Idea:
            data = new WorkFlowNode(nodeId, parentId, '审批意见', FlowNodeTypeEnum.Idea, childNode?.value ?? null);
            var agree = new WorkFlowNode(uuid(), nodeId, '审批意见', FlowNodeTypeEnum.Agree);
            var noAgree = new WorkFlowNode(uuid(), nodeId, '审批意见', FlowNodeTypeEnum.NoAgree);
            data.conditionNodes.push(agree, noAgree);
            break;
    }
    childNode.value = data;
    visible.value = false;
}
</script>